const mongoose = require('mongoose');


mongoose.connect('mongodb://127.0.0.1:27017/users');

//建议用once连接数据库 once 只链接一次
mongoose.connection.once('open',() => {
    //创建文档（表）的结构
    let mySonSchema = mongoose.Schema({
        name:String,
        age:Number
    });
    //创建模板对象
    let mySonModel = mongoose.model('mysons',mySonSchema);

    //插入文档
    mySonModel.create({
        name:"曹敬文",
        age:18
    } ,(err,data) => {
        if (err) {
            console.log(err);
            return;
        }
        console.log(data);
        //断开连接
        mongoose.disconnect();
    });
});
mongoose.connection.on('close',() => {
    console.log('连接关闭');
});
mongoose.connection.on('error',() => {
    console.log('连接失败');
});